package com.example.roadeyeserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.roadeyeserver.constant.ProjectConstant;
import com.example.roadeyeserver.entity.AreaInfo;
import com.example.roadeyeserver.entity.vo.AreaInfoVO;
import com.example.roadeyeserver.mapper.AreaInfoMapper;
import com.example.roadeyeserver.result.Result;
import com.example.roadeyeserver.service.AreaInfoService;
import com.example.roadeyeserver.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AreaInfoServiceImpl extends ServiceImpl<AreaInfoMapper, AreaInfo> implements AreaInfoService {

    @Resource
    private AreaInfoMapper areaInfoMapper;

//    @PostConstruct
    public void createTable() {
        log.info("开始创建区域信息表area_info");
        areaInfoMapper.createTable();
        log.info("区域信息表area_info创建完成");
        List<AreaInfo> list = this.list();
        log.info("区域信息表area_info数据量:{},数据:{}", list.size(), list);
    }

    @Override
    public Result<List<AreaInfoVO>> getNBAreaInfoList() {
        // 获取所有区县
        List<AreaInfo> areaList = this.list();
        // 筛选获得宁波市内的区县
        List<AreaInfo> nBList = areaList.stream()
                .filter(item -> StringUtils.isNotEmpty(item.getAreaCode()) &&
                        item.getAreaCode().startsWith(ProjectConstant.NING_BO_AREA_CODE_START) &&
                        !item.getAreaCode().equals(ProjectConstant.NING_BO_CODE))
                .collect(Collectors.toList());
        List<AreaInfoVO> nBListVO = BeanUtil.copyToList(nBList, AreaInfoVO.class);
        return ResultUtil.success(nBListVO);
    }
}
